define(['tech-store-models/item'], function(Item) {
    'use strict';

    var Store = (function () {
        function Store(name) {
            if (name.length < 6 || name.length > 30) {
                throw Error('You can declare items with name length longer than 5 and shorter than 31 symbols');
            }

            this._name = name;
            this._items = [];
            this._copiedItems = [];
        }

        Store.prototype.addItem = function (item) {
            if (!(item instanceof Item)) {
                throw Error('You can fill the store only with valid items');
            }

            this._items.push(item);

            return this;
        };

        Store.prototype.getAll = function () {
            this._copiedItems = this._items.slice(0, this._items.length);

            this._copiedItems.sort(function(a, b) { // da go iznesa vuv funkciq
                //var textA = a[_name].toUpperCase();
                //var textB = b[_name].toUpperCase();
                //return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
                return a._name.localeCompare(b._name);
            });

            return this._copiedItems;
        };

        Store.prototype.getSmartPhones = function () {
            this._copiedItems = [];

            //for (var i = 0; i < this._items.length; i++) {
            //    if (this._items[i]._type.toString() === 'smart-phone') {
            //        this._copiedItems.push(this._items[i])
            //    }
            //}

            var self = this;

            this._items.forEach(function (item) {
                if (item._type.toString() === 'smart-phone') {
                    self._copiedItems.push(item)
                }
            });

            this._copiedItems.sort(function(a, b) { // vuv funkciq
                return a._name.localeCompare(b._name);
            });

            return this._copiedItems;
        };

        Store.prototype.getMobiles = function () {
            this._copiedItems = [];

            var self = this;

            this._items.forEach(function (copiedItem) {
                if (copiedItem._type === 'smart-phone' || copiedItem._type === 'tablet') {
                    self._copiedItems.push(copiedItem)
                }
            });

            this._copiedItems.sort(function(a, b) { // vuv funkciq
                return a._name.localeCompare(b._name);
            });

            return this._copiedItems;
        };

        Store.prototype.getComputers = function () {
            this._copiedItems = [];

            var self = this;

            this._items.forEach(function (copiedItem) {
                if (copiedItem._type === 'notebook' || copiedItem._type === 'pc') {
                    self._copiedItems.push(copiedItem)
                }
            });

            this._copiedItems.sort(function(a, b) { // vuv funkciq
                return a._name.localeCompare(b._name);
            });

            return this._copiedItems;
        };

        Store.prototype.filterItemsByType = function (typeForFilter) {
            this._copiedItems = [];

            var self = this;

            this._items.forEach(function (copiedItem) {
                if (copiedItem._type === typeForFilter) {
                    self._copiedItems.push(copiedItem)
                }
            });

            this._copiedItems.sort(function(a, b) { // vuv funkciq
                return a._name.localeCompare(b._name);
            });

            return this._copiedItems;
        };

        Store.prototype.filterItemsByName = function (nameForFilter) {
            this._copiedItems = [];

            var self = this;

            this._items.forEach(function (copiedItem) {
                if (copiedItem._name.toUpperCase().indexOf(nameForFilter.toUpperCase()) > -1) {
                    self._copiedItems.push(copiedItem)
                }
            });

            this._copiedItems.sort(function(a, b) { // vuv funkciq
                return a._name.localeCompare(b._name);
            });

            return this._copiedItems;
        };

        Store.prototype.countItemsByType = function () {
            var result = {
                'accessory': 0,
                'smart-phone': 0,
                'notebook': 0,
                'pc': 0,
                'tablet': 0
            };

            var self = this;

            this._items.forEach(function (itemToCount) {
                result[itemToCount._type]++;
            });

            return result;
        };

        Store.prototype.filterItemsByPrice = function (objWithMinAndMax) {
            var theObj = objWithMinAndMax || { min: 0, max: Number.POSITIVE_INFINITY};

            if (objWithMinAndMax === 'undefined') {
                theObj.min = objWithMinAndMax.min !== 'undefined' ? objWithMinAndMax.min : 0;
                theObj.max = objWithMinAndMax.max !== 'undefined' ? objWithMinAndMax.max : Number.POSITIVE_INFINITY;
            }


            this._copiedItems = [];

            var self = this;

            this._items.forEach(function (copiedItem) {
                if (copiedItem._price > theObj.min ||
                    copiedItem._price < theObj.max) {
                    self._copiedItems.push(copiedItem)
                }
            });

            this._copiedItems.sort(function(a, b) {
                return a._price - b._price;
            });

            return this._copiedItems;
        };

        return Store;
    }());

    return Store;
});